<?php

class View {

    private $variables;
    private $indice = 0;
    private $layout;

    public function __construct($layout) {
        $this->layout = APP_DIR_LAYOUTS .'/'.$layout;
    }

    public function __toString() {
        $this->getLayout();
        $this->processLayout();
        return $this->layout;
    }

    public function render() {
        $this->getLayout();
        $this->processLayout();
        echo $this->layout;
    }

    public function setLabel($nombre, $valor) {
        $this->variables[$this->indice]['nombre'] = $nombre;
        $this->variables[$this->indice]['valor'] = $valor;
        $this->indice++;
    }

    private function getLayout() {

        $this->layout = file_get_contents($this->layout);
    }

    private function processLayout() {

        //proceso el layout del modulo
        for ($i = 0; $i < $this->indice; $i++) {
            if (!substr_count($this->layout, '{' . $this->variables[$i]['nombre'] . '}')) {
                throw new Exception('No existe <b>{' . $this->variables[$i]['nombre'] . '}</b> en el layout');
                exit();
            }
            $this->layout = str_replace('{' . $this->variables[$i]['nombre'] . '}', $this->variables[$i]['valor'], $this->layout);
//            $this->layout = str_replace(' href=', ' onclick="window.setInterval(\'cargando()\',300)" href=', $this->layout);
//            $this->layout = str_replace('location.href', 'window.setInterval(\'cargando()\',300);location.href', $this->layout);
        }
    }

}

?>
